﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Thor.Common;
using Thor.Domain;

namespace Thor.SharePoint2007.WebServices
{
    internal class SPEventRequest
    {
        private static XmlDocument eventDoc;

        public static XmlNode GetEventRequest(AppointmentRequest request)
        {
            eventDoc = new XmlDocument();

            var batchNode = GetBatchNode();
            var methodNode = GetMethodNode();

            var titleElement = GetTitleElement(request.Subject);
            var locationElement = GetLocationElement(string.Empty);
            var descriptionElement = GetDescriptionElement(request.Subject);
            var eventDateElement = GetEventDateElement(request.StartTime.ToSharePointString());
            var endDateElement = GetEndDateElement(request.EndTime.ToSharePointString());
            var allDayEventElement = GetAllDayEventElement("0");

            methodNode.AppendChild(titleElement);
            methodNode.AppendChild(locationElement);
            methodNode.AppendChild(descriptionElement);
            methodNode.AppendChild(eventDateElement);
            methodNode.AppendChild(endDateElement);
            methodNode.AppendChild(allDayEventElement);

            batchNode.AppendChild(methodNode);
            eventDoc.AppendChild(batchNode);

            return batchNode;
        }

        private static XmlNode GetBatchNode()
        {
            return eventDoc.CreateNode(XmlNodeType.Element, "Batch", "");
        }

        private static XmlNode GetMethodNode()
        {
            XmlNode  methodNode = eventDoc.CreateNode(XmlNodeType.Element, "Method", "");

            XmlAttribute idAttr = eventDoc.CreateAttribute("ID");
            idAttr.Value = "1";

            XmlAttribute cmdAttr = eventDoc.CreateAttribute("Cmd");
            cmdAttr.Value = "New";

            methodNode.Attributes.Append(idAttr);
            methodNode.Attributes.Append(cmdAttr);

            return methodNode;
        }

        private static XmlElement GetTitleElement(string title)
        {
            return GetFieldElement("Title", title);
        }

        private static XmlElement GetLocationElement(string location)
        {
            return GetFieldElement("Location", location);
        }

        private static XmlElement GetDescriptionElement(string description)
        {
            return GetFieldElement("Description", description);
        }

        private static XmlElement GetEventDateElement(string eventDate)
        {
            return GetFieldElement("EventDate", eventDate);
        }

        private static XmlElement GetEndDateElement(string endDate)
        {
            return GetFieldElement("EndDate", endDate);
        }

        private static XmlElement GetAllDayEventElement(string allDayEvent)
        {
            return GetFieldElement("fAllDayEvent", allDayEvent);
        }

        private static XmlElement GetFieldElement(string fieldName, string fieldValue)
        {
            XmlElement fieldElement = eventDoc.CreateElement("Field");
            
            XmlAttribute nameAttribute = eventDoc.CreateAttribute("Name");
            nameAttribute.Value = fieldName;
            fieldElement.Attributes.Append(nameAttribute);
            fieldElement.InnerText = fieldValue;
            
            return fieldElement;
        }
    }
}